package com.sunwayworld.basemodule.coredata.test.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.test.dao.LimsPossibleresultDao;
import com.sunwayworld.basemodule.coredata.test.bean.LimsPossibleResultBean;
import com.sunwayworld.basemodule.coredata.test.service.LimsPossibleResultService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

@Repository
@GikamBean
public class LimsPossibleResultServiceImpl implements LimsPossibleResultService {

    @Autowired
    private LimsPossibleresultDao limsPossibleresultDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsPossibleresultDao getDao() {
        return limsPossibleresultDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsPossibleResultBean possibleResultBean = jsonWrapper.parseUnique(LimsPossibleResultBean.class);
        possibleResultBean.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(possibleResultBean);
        return possibleResultBean.getId();
    }

    @Transactional
    @Override
    public void deleteByAnalyteId(Long analyteId) {
        if(analyteId == null) return;
        LimsPossibleResultBean possibleResultBean = new LimsPossibleResultBean();
        possibleResultBean.setAnalyteId(analyteId);
        getDao().deleteBy(possibleResultBean, "analyteId");
    }
}
